package com.iraeta.uitl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.alibaba.fastjson.JSONArray;

public class Util {
    public static String parseListToString(List<? extends Object> list) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if (i > 0) {
                sb.append(",");
            }
            sb.append(list.get(i).toString());
        }
        return sb.toString();
    }
    public static String parseIntsToString(Object... paras) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < paras.length; i++) {
            if (i > 0) {
                sb.append(",");
            }
            sb.append(paras[i].toString());
        }
        return sb.toString();
    }
    public static String parseJSONArrayToString(JSONArray ids) {
        if(ids.size() == 0){
            return "";
        }
        boolean first = true;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ids.size(); i++) {
            if (!first) {
                sb.append(",");
            }
            sb.append(ids.getString(i));
            first = false;
        }
        return sb.toString();
    }
    public static JSONArray parseStringToJSONArray(String splitStr) {
        String[] ids = splitStr.split(",");
        JSONArray jsonArray = new JSONArray();
        int i = 0;
        for(String idStr : ids){
            jsonArray.add(i, Integer.parseInt(idStr));
            i++;
        }
        return jsonArray;
    }
    public static int parseIntFromMap(Map<String, Object> row, String key) {
        if(!row.containsKey(key)){
            return 0;
        }
        String val = row.get(key).toString();
        if(StrUtil.isEmpty(val)){
            return 0;
        }
        return Integer.parseInt(val);
    }
    public static String parseStringFromMap(Map<String, Object> row, String key) {
        if (!row.containsKey(key))
            return null;
        Object o = row.get(key);
        if (o == null) {
            return null;
        }
        return o.toString();
    }
    public static String toStackTrace(Exception e){
        StringWriter sw  = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try {
            e.printStackTrace(pw);
            return sw.toString();
        }catch (Exception e1){
            return "未知错误";
        }
    }

    public static String getBusinessNo() {
        StringBuffer sb = new StringBuffer();
        String dateStr = DateUtil.format(new Date(),"MMddHHmmssSSS");
        int random = new Random().nextInt(9000) + 1000;
        sb.append(dateStr).append(String.valueOf(random));
        return sb.toString();
    }

    public static int getRandomInt(){
        int a = - new Random().nextInt(1000000);
        return a ;
    }

    public static String getName(String fileName)
    {
        if (fileName == null)
        {
            return null;
        }
        int lastUnixPos = fileName.lastIndexOf('/');
        int lastWindowsPos = fileName.lastIndexOf('\\');
        int index = Math.max(lastUnixPos, lastWindowsPos);
        return fileName.substring(index + 1);
    }
}
